<template>
	<layout :navObj="navObj">
		<block slot="medi">
			<view class="box">
				<input type="tel" v-model="phone" />
				<button @click="dial">拨号</button>
			</view>

			<a :href="'tel:'+phone">打电话</a>

			<view class="qqqqqq">
				<button @tap="endRecord">停止录音</button>

				<button @tap="playVoice">播放录音</button>


				<button @tap="startVoice">开始录音</button>
			</view>
		</block>
		<block slot="bottom">
			<view style="height: 44px;background-color: #686803;"></view>
		</block>
	</layout>

</template>

<script>
	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();
	innerAudioContext.autoplay = true;

	export default {
		data() {
			return {
				phone: '10086',
				voicePath: '',
				top: this.StatusBar,
				navObj: {
					title: '录音测试',

				},
				timer: null,
			}
		},
		onLoad() {
			let self = this;
			recorderManager.onStop(function(res) {
				console.log("res", res)
				let voicePath = res.tempFilePath;
				self.voicePath = voicePath;
				uni.showToast({
					icon: 'loading',
					title: "请稍后...",
					duration: 0
				});
				setTimeout(() => {
					uni.hideToast();
				}, 1000)
			});
			recorderManager.onError((err) => {
				console.log(err, '错误事件')
			})
			// #ifdef APP-PLUS
			self.getCallStatus();
			// #endif
		},
		methods: {
			dial() {
				console.log('点击拨号按钮了');
				wx.makePhoneCall({
					phoneNumber: this.phone, // 要拨打的手机号
					success(re) {
						console.log(re, '拨号成功');
					},
					fail(er) {
						// 拨打电话失败的逻辑
						console.log('拨号失败', er);
					}
				})
			},
			// 手动开始录音
			startVoice() {
				recorderManager.start();
			},
			// 手动停止录音功能
			endRecord() {
				console.log('录音结束');
				recorderManager.stop();
			},

			// 播放录音
			playVoice() {
				console.log('点击录音播放', this.voicePath);
				if (this.voicePath) {
					innerAudioContext.src = this.voicePath;
					innerAudioContext.onPlay(() => {
						console.log('手动开始播放');
					});
					innerAudioContext.onError((res) => {
						console.log('播放错误', res);
					});
				}
			},

			getCallStatus() {
				let that = this;
				let maintest = plus.android.runtimeMainActivity();
				let Contexttest = plus.android.importClass("android.content.Context");
				let telephonyManager = plus.android.importClass("android.telephony.TelephonyManager");
				let telManager = plus.android.runtimeMainActivity().getSystemService(Contexttest.TELEPHONY_SERVICE);
				let receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
					onReceive: function(Contexttest, intent) {
						plus.android.importClass(intent);
						let phoneStatus = telManager.getCallState(); //电话状态 0->空闲状态 1->振铃状态 2->通话存在
						console.log(phoneStatus, '通话状态');
						switch (phoneStatus) {
							case 0:
								console.log("3、电话挂断，上传录音")
								// 结束录音
								recorderManager.stop();
								break;
							case 1:
								console.log('1、振铃状态');
								break;
							case 2:
								console.log('2、通话存在')
								setTimeout(() => {
									console.log('开始自动录音');
									recorderManager.start();
								}, 1000);
								break;
						}
					}
				});
				let IntentFilter = plus.android.importClass('android.content.IntentFilter');
				let filter = new IntentFilter();
				filter.addAction(telephonyManager.ACTION_PHONE_STATE_CHANGED);
				maintest.registerReceiver(receiver, filter);
			},

		}
	}
</script>

<style lang="less" scoped>
	button {
		padding: 8px;
	}

	.box {
		width: 100%;
		padding: 12px 15px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;

		input {
			width: 80%;
			border: 1px solid #d0d0d0;
			box-sizing: border-box;
			padding: 4px 7px;
			height: 100%;
		}

		button {
			width: 15%;
			box-sizing: border-box;
			background-color: #3c8448;
			color: #fff;
		}
	}

	.qqqqqq {
		padding: 15px;
		box-sizing: border-box;

		button {
			margin-bottom: 15px;
			background-color: #a084a2;
			color: #fff;
		}
	}
</style>